﻿using JsonFx.Json;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class EventData
{
	private static EventData instance;

	public InGameEventData[] inGameEvent;
	public AttendData[] attend;
	public FlyEventData[] flyEvent;
	public BonusEventData[] bonusEvent;
	public OnetimeEventData[] onetimeEvent;

	private static EventData Inst { get { Init(); return instance; } }

	public static InGameEventData[] InGameEvent { get { return Inst.inGameEvent; } }
	public static AttendData[] Attend { get { return Inst.attend; } }
	public static FlyEventData[] FlyEvent { get { return Inst.flyEvent; } }
	public static BonusEventData[] BonusEvent { get { return Inst.bonusEvent; } }
	public static OnetimeEventData[] OnetimeEvent { get { return Inst.onetimeEvent; } }

	public static void Init()
	{
		if (instance == null)
		{
            string data = null;
            using (System.IO.StreamReader r = System.IO.File.OpenText(Application.persistentDataPath + "/event.json"))
            {
                data = r.ReadToEnd();
            }
            instance = JsonReader.Deserialize<EventData>(data);
            Debug.Log("eventdata init");
		}
	}

	public static List<InGameEventData> GetActiveEventList(long currentTick)
	{
		return InGameEvent.Where(d => currentTick >= d.startTick && currentTick < d.endTick).ToList();
	}

	public static bool IsBonusEventActive
	{
		get
		{
			var eList = GetActiveEventList(System.DateTime.Now.Ticks).Where(d => d.rewardID == 300).ToList();

			return eList.Count > 0;
		}
	}
}

public class InGameEventData
{
	public int ID;
	public long startTick;
	public long endTick;
	public string start { get { return ""; } set { startTick = System.DateTime.Parse(value).Ticks; } }
	public string end { get { return ""; } set { endTick = System.DateTime.Parse(value).Ticks; } }
	public byte type;
	public short typeVal;
	public short rewardID;
	public float rewardVal;
}

public class AttendData
{
	public int month;
	public short count;
	public short rewardType;
	public int rewardVal;
}

public class FlyEventData
{
	public short birdID;
	public short cooltime;
	public short weight;
	public int itemID;
}

public class BonusEventData
{
	public int ID;
	public byte type;
	public short amount;
	public float bonus;
}

public class OnetimeEventData
{
	public short amount;
	public float bonus;
}
